package com.yc.web.controller;

import com.yc.services.OSSService;
import com.yc.services.PsychologyAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; // ⬅️ **导入 MediaType**
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux; // ⬅️ **导入 Flux**

@RestController
@RequestMapping("/api/ai")
public class PsychologyController {

    @Autowired
    private OSSService ossService;

    @Autowired
    private PsychologyAnalysisService analysisService;

    @PostMapping(value = "/psychology/analyze/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) // ⬅️ **修改路径和produces**
    public Flux<String> analyzeImageStream(@RequestParam("image") MultipartFile image,
                                         @RequestParam("userId") String userId) { // ⬅️ **添加userId参数**
        // 1. 上传图片到OSS
        String imageUrl = ossService.uploadImage(image);

        // 2. 调用心理分析服务，返回 Flux<String>，传递userId参数
        return analysisService.analyzeImageStream(imageUrl, userId); // ⬅️ **传递userId参数**
    }
    
    /**
     * 获取用户心理分析历史记录
     * @param userId 用户ID
     * @return 心理分析历史数据或404
     */
    @GetMapping("/psychology/history")
    public ResponseEntity<String> getAnalysisHistory(@RequestParam("userId") String userId) {
        String historyData = analysisService.getAnalysisHistory(userId);
        
        if (historyData != null) {
            return ResponseEntity.ok(historyData);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}